﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Giaiphongmatbang.BLL;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        SupportMoveRepository repository = new SupportMoveRepository();
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }

        public void BindData()
        {
            StringBuilder str = new StringBuilder("<table id='treeMove' width='100%' class='table'>");
            str.AppendLine("<tr>"
                + "<th>Loại di chuyển</th>"
                + "<th>Giá tiền</th>"
                + "<th>&nbsp;</th>"
                + "</tr>");
            var list = repository.GetByParentID(0);
            foreach (var item in list)
            {
                StringBuilder node = new StringBuilder("<tr id='"+ item.SupportMoveID+"'><td>" + item.SupportName + "</td><td>" + item.Cost + "</td><td><input type='checkbox'/></td></tr>");
                BindDataChild(node, item.SupportMoveID);
                str.AppendLine(node.ToString());
            }
            str.AppendLine("</table");
            ltr.Text = str.ToString();

        }
        protected void BindDataChild(StringBuilder parent, int ParentID)
        {
            var list = repository.GetByParentID(ParentID);
            
            foreach (var item in list)
            {
                StringBuilder node = new StringBuilder("<tr id='" + item.SupportMoveID + "' class='child-of-"+item.ParentID+"'><td>" + item.SupportName + "</td><td>" + item.Cost + "</td><td><input type='checkbox'/></td></tr>");
                BindDataChild(node, item.SupportMoveID);
                parent.AppendLine(node.ToString());
            }
        }  
    }
}